
function stringToExamples(value){
  let reg = "<input>([\\s\\S]*?)</input><output>([\\s\\S]*?)</output>";
  let re = RegExp(reg,"g");
  let objList = []
  let tmp;
  while(tmp=re.exec(value)){
    objList.push({input:tmp[1],output:tmp[2]})
  }
  return objList
}

function examplesToString(objList){
  if(objList.length == 0){
    return "";
  }
  let result=""
  for(let obj of objList){
    result+= "<input>"+obj.input+"</input><output>"+obj.output+"</output>"
  }
  return result
}

function getProblemCase(index,input,output){
  return `##### 测试用例-${index}

**输入：**

\`\`\`
${input}
\`\`\`

**输出：**

\`\`\`
${output}
\`\`\`
`
}

function analysisProblemCase(examples){
  let res = "";
  const t = stringToExamples(examples);
  if(t!=null && t.length > 0){
    for (let i = 0; i < t.length; i++) {
      res = res + getProblemCase(i+1,t[i].input,t[i].output) + "\n";
    }
  }
  return res;
}

function isNoEmpty(str){
  return str!=undefined && str!=null && str.trim()!=''
}

function getProblemDescribe(problem) {

  let ans = `## ${problem.title}
   > 时间限制：C/C++ ${problem.timeLimit}MS
   > 内存限制：C/C++ ${problem.memoryLimit / 1024}MB
   > 难度：${problem.difficulty && problem.difficulty.name}
   > 分数：${problem.ioScore}
   > 出题人：${problem.author && problem.author.userName || '未知'}\n`

  if (isNoEmpty(problem.description))
    ans = ans + `\n### 题目描述\n\n${problem.description || ""}\n`
  if (isNoEmpty(problem.input))
    ans = ans + `\n### 输入描述\n\n${problem.input || ""}\n`
  if (isNoEmpty(problem.output))
    ans = ans + `\n### 输出描述\n\n${problem.output || ""}\n`
  if (isNoEmpty(problem.examples))
    ans = ans + `\n### 输入输出样例\n\n${analysisProblemCase(problem.examples)}\n`
  if (isNoEmpty(problem.prompt))
    ans = ans + `\n### 提示\n\n${problem.prompt || ""}\n`
  return ans;

}

function getContestProblemDescribe(problem) {

  let ans = `## ${problem.title}
   > 时间限制：C/C++ ${problem.timeLimit}MS
   > 内存限制：C/C++ ${problem.memoryLimit / 1024}MB
   > 分数：${problem.ioScore}
   > 出题人：[${problem.author && problem.author.userName || '未知'}]()\n`

  if (isNoEmpty(problem.description))
    ans = ans + `\n### 题目描述\n\n${problem.description || ""}\n`
  if (isNoEmpty(problem.input))
    ans = ans + `\n### 输入描述\n\n${problem.input || ""}\n`
  if (isNoEmpty(problem.output))
    ans = ans + `\n### 输出描述\n\n${problem.output || ""}\n`
  if (isNoEmpty(problem.examples))
    ans = ans + `\n### 输入输出样例\n\n${analysisProblemCase(problem.examples)}\n`
  if (isNoEmpty(problem.prompt))
    ans = ans + `\n### 提示\n\n${problem.prompt || ""}\n`
  return ans;
}

export {
  stringToExamples,
  examplesToString,
  getProblemDescribe,
  getContestProblemDescribe,
  getProblemCase,
}
